package com.example.desionMode.bank.normal;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;

/**
 * @author houyangfan
 * @version 1.0
 * @date 2022/8/5 10:36
 *
 *  普通做法
 */
public class BankService {

    public static String createUser(String name, String idCard, Integer age, String mobile){

        StringBuilder stringBuilder = new StringBuilder();
        // 名称 字符串靠左，其余的补充_
        stringBuilder.append(String.format("%-10s", name).replace(" ", "_"));
        // 身份证
        stringBuilder.append(String.format("%-18s", idCard).replace(" ", "_"));
        // 年龄 数字靠右，其余补充_
        stringBuilder.append(String.format("%05d", age));
        // 手机号
        stringBuilder.append(String.format("%-11s", mobile).replace(" ", "_"));

        return stringBuilder.toString();
    }

    public static String pay(Integer uid, BigDecimal amount){

        StringBuilder stringBuilder = new StringBuilder();
        // 用户id 数字靠右，其余补充_
        stringBuilder.append(String.format("%020d", uid));
        // 金额 向下舍入2位到分，以分为单位，作为数字靠右，多余的地方用0填充
        stringBuilder.append(String.format("%010d", amount.setScale(2, RoundingMode.DOWN).multiply(new BigDecimal("100")).longValue()));

        return stringBuilder.toString();
    }

    public static void main(String[] args) {

        System.out.println(commonDisplay("310"));
    }


        private static final int SIZE = 4;
        private static final String SYMBOL = "*";

        /**
         * 通用脱敏方法
         * @param value
         * @return
         */
        public static String commonDisplay(String value) {
            if (null == value || "".equals(value)) {
                return value;
            }
            int len = value.length();
            int pamaone = len / 2;
            int pamatwo = pamaone - 1;
            int pamathree = len % 2;
            StringBuilder stringBuilder = new StringBuilder();
            if (len <= 2) {
                if (pamathree == 1) {
                    return SYMBOL;
                }
                stringBuilder.append(SYMBOL);
                stringBuilder.append(value.charAt(len - 1));
            } else {
                if (pamatwo <= 0) {
                    stringBuilder.append(value.substring(0, 1));
                    stringBuilder.append(SYMBOL);
                    stringBuilder.append(value.substring(len - 1, len));
                } else if (pamatwo >= SIZE / 2 && SIZE + 1 != len) {
                    int pamafive = (len - SIZE) / 2;
                    stringBuilder.append(value.substring(0, pamafive));
                    for (int i = 0; i < SIZE; i++) {
                        stringBuilder.append(SYMBOL);
                    }
                    if ((pamathree == 0 && SIZE / 2 == 0) || (pamathree != 0 && SIZE % 2 != 0)) {
                        stringBuilder.append(value.substring(len - pamafive, len));
                    } else {
                        stringBuilder.append(value.substring(len - (pamafive + 1), len));
                    }
                } else {
                    int pamafour = len - 2;
                    stringBuilder.append(value.substring(0, 1));
                    for (int i = 0; i < pamafour; i++) {
                        stringBuilder.append(SYMBOL);
                    }
                    stringBuilder.append(value.substring(len - 1, len));
                }
            }
            return stringBuilder.toString();
        }


}
